/*
 * 
 */

package kds.network.http;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import kds.network.HTTPRequest;



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


/**
 * The Class HTTPResponseData is Data Object It use for save a bytes array give from
 * InputStream
 * 
 * @see kds.network.HTTPRequest
 * 
 * @author h0akd93
 */
public class HTTPResponseData {

	/** The bytes array data */
	private byte[] bytes = null;
	private boolean expired = false;

	/**
	 * Default instantiates a new empty KDdata.
	 */
	public HTTPResponseData() {
		bytes = new byte[0];
	}

	/**
	 * Instantiates a new KDdata from other KDdata
	 * 
	 * @param aData
	 *            is other HTTPResponseData
	 */
	public HTTPResponseData(HTTPResponseData aData) {
		bytes = aData.getBytes();
	}

	/**
	 * Instantiates a new HTTPResponseData from file you can use it to read cache
	 * 
	 * @param file
	 *            the input file
	 */
	public HTTPResponseData(File file) throws Exception {
		int len = (int) file.length();
		bytes = new byte[len];
		FileInputStream finput = new java.io.FileInputStream(file);
		finput.read(bytes);
		try {
			finput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Instantiates a new HTTPResponseData from URLrequest and InputStream While read from
	 * input stream it request be cancel the loop will be break and close
	 * connection
	 * 
	 * @see kds.network.HTTPRequest#run() <b>Main Request Runnable</b>
	 * 
	 * @param request
	 *            the request
	 * @param inputStream
	 *            the input stream
	 * @throws IOException
	 *             Signals that an I/O exception has occurred. This Error will
	 *             be return for request deldegate
	 */
	public HTTPResponseData(HTTPRequest request, InputStream inputStream)
			throws IOException {

		ByteArrayOutputStream buffer = new ByteArrayOutputStream();

		int nRead;
		// buffer 2048 byte
		byte[] data = new byte[2048];
		while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
			if (request != null && request.isCancelThread())
				break;
			buffer.write(data, 0, nRead);
		}
		buffer.flush();
		bytes = buffer.toByteArray();

	}

	public HTTPResponseData(InputStream inputStream) throws IOException {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		int nRead;
		// buffer 2048 byte
		byte[] data = new byte[2048];
		while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
			buffer.write(data, 0, nRead);
		}
		buffer.flush();
		bytes = buffer.toByteArray();

	}

	/**
	 * Gets the bytes.
	 * 
	 * @return the bytes
	 */
	public byte[] getBytes() {
		return bytes;
	}

	/**
	 * Convert bytes array to string
	 */
	@Override
	public String toString() {
		return new String(getBytes());
	}

	/**
	 * Convert bytes array to bitmap.
	 * 
	 * @return the bitmap
	 */
	public Bitmap toBitmap() {
		return BitmapFactory.decodeByteArray(getBytes(), 0, bytes.length);
	}

	/**
	 * Sets the bytes.
	 * 
	 * @param data
	 *            the new bytes
	 */
	public void setBytes(byte[] data) {
		bytes = new byte[data.length];
		for (int i = 0; i < data.length; i++) {
			bytes[i] = data[i];
		}
	}

	/**
	 * Write bytes array data to file.
	 * 
	 * 
	 * @param aFile
	 *            the a file for save
	 * @return true, if successful
	 */
	public boolean writeToFile(File aFile) {
		try {
			FileOutputStream fos = new FileOutputStream(aFile);
			fos.write(bytes);
			fos.close();
		} catch (Exception exc) {
			exc.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return the expired
	 */
	public boolean isExpired() {
		return expired;
	}

	/**
	 * @param expired
	 *            the expired to set
	 */
	public void setExpired(boolean expired) {
		this.expired = expired;
	}
}
